

require "palace.lua"
originFrames = {}
drawModel = nil
totalElapsedTime = 0
triggerTime = 0
triggerIndex = -1
totalframeIndex = 0 --前指针
oldFrameIndex = 0 --后指针
function initialize(scene)

  local node1 = getSnapshot2(scene,0.3)

  local frameMesh = Mesh.createQuadFullscreen()
  drawModel = Model.create(frameMesh)
  frameMesh:release()

  local material = Material.createWithShaderFile("res/shaders/passthrough.vert", "res/shaders/passthrough.frag", Nil)
  material:getParameter("u_worldViewProjectionMatrix"):setMatrix(Matrix.identity())
  material:getParameter("u_texture"):setSampler(node1:getSnapshot())
  drawModel:setMaterial(material, -1)

  PalaceEffect:init(scene,EffectType.EffectType_3x3)

  for k=1,8 do
   PalaceEffect:add(node1:getSampler(),k)
  end

  PalaceEffect:commit()
  PalaceEffect:isShowEnabled(true)
end

function frameReady(scene, elapsedTime)
  -- totalElapsedTime = totalElapsedTime + elapsedTime
  -- totalIndex = math.floor(totalElapsedTime/50)

  local index = math.floor(triggerTime/70)
  print("indexindex"..index)
  
  if triggerIndex ~= index then
    triggerSlowmotion(index,scene)
    triggerIndex = index
  end
  triggerTime = triggerTime + elapsedTime
end

function triggerSlowmotion(frameIndex, scene)
  
   local index = frameIndex % 1 --控制刷新率
  -- save frame
  if totalframeIndex-oldFrameIndex < 50 then --总共50帧
    local state = FrameBufferBindingState.create()
    local frameBuffer = FrameBuffer.create("STORED_FB_1", math.floor(scene:getResolution().x/3), math.floor(scene:getResolution().y/3), TextureFormat.RGBA)
    frameBuffer:bindWithViewport(true)
    drawModel:draw()
    state:restore()
    totalframeIndex = totalframeIndex+1
    originFrames[totalframeIndex] = frameBuffer
    print("originFramesoriginFrames"..#originFrames)
  end
  

  if(totalframeIndex>9) then
    for i=1,8 do
      local slowFrameBuffer = getFrameBufferWithIndex(totalframeIndex-i+1) --取到第一帧
      local frameSampler = TextureSampler.createWithTexture(slowFrameBuffer:getRenderTarget(0):getTexture())
      if frameSampler ~= nil then
        PalaceEffect:update(frameSampler,i)
        frameSampler:release()
       
      else
        print("frameSampler nil")
      end
       
    end
 
    if index==0 and totalframeIndex>22  then
      removeBufferWithIndex(oldFrameIndex)
      oldFrameIndex = oldFrameIndex+1
    end
   
  end 
   

  
  
end
function getFrameBufferWithIndex(index) --得到第一个
 
    if originFrames[index] ~= nil then
      return originFrames[index]
    end
  return nil
end

function getFrameBuffer() --得到第一个
  for i = 1, #originFrames do
    if originFrames[i] ~= nil then
      print("getFrameBuffer"..i)
      return originFrames[i]
    end
  end
  return nil
end
function removeBufferWithIndex(index) --移除第一个
  
    if originFrames[index] ~= nil then
      originFrames[index]:release()
      originFrames[index] = nil
      return
    end
  
end
function removeHeadBuffer() --移除第一个
  for i = 1, #originFrames do
    if originFrames[i] ~= nil then
      originFrames[i]:release()
      originFrames[i] = nil
      return
    end
  end
end

function clearBuffers() --清除所有buffers
  for i = 1, #originFrames do
    if originFrames[i] ~= nil then
      originFrames[i]:release()
      originFrames[i] = nil
    end
  end
end

function addNodeAndRelease(scene, node)
  scene:addNode(node)
  node:release()
  return node
end


function getSnapshot2(scene, bufferScale)
  local node = KuruSnapshotNode.create()
  scene:addNode(node)
  node:release()
  node:setFrameBufferScale(bufferScale, bufferScale)
  return node
end